package listdemo;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2023-10-31
 * Time：15:26
 */
public class MySingleList {
    static class ListNode {
        public int value;
        public ListNode next;

        public ListNode(int value) {
            this.value = value;
        }
    }

    public ListNode head;//代表单链表的单节点的引用
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            return;
        }

        ListNode cur = head;
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    private boolean checkIndexInvalid(int index) {
        return index < 0 || index > this.size();
    }
    public void addIndex(int index,int data){
        if (checkIndexInvalid(index)) {
            throw new SingleListOutOfIndexException("任意位置插入时下标越界");
        }
        if (index == 0) {
            addFirst(data);
        }
        if (index == size()) {
            addLast(data);
        }
        ListNode node = new ListNode(data);

        ListNode cur = head;
        int step = index - 1;
        while (step > 0) {
            step--;
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next = node;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while (cur != null) {
            if (cur.value == key) {
                return true;
            }
            cur = cur.next;
        }

        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if (head == null) {
            System.out.println("链表为空，不能进行删除");
            return;
        }
        if (head.value == key) {
            head = head.next;
            return;
        }
        ListNode cur = head;
        while(cur.next!=null) {
            if (cur.next.value == key) {
                ListNode del = cur.next;
                cur.next = del.next;
                return;
            }
            cur = cur.next;
        }
        System.out.println("没有关键字为key的节点，删除失败");
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(this.head == null) {
            return;
        }

        ListNode cur = this.head.next;
        ListNode prev = this.head;

        while (cur != null) {
            if(cur.value == key) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        //单独处理了头节点
        if(this.head.value == key) {
            head = head.next;
        }
    }
    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    public void display(){
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.value + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void displayByHead(ListNode newHead) {
        ListNode cur = newHead;
        while (cur != null) {
            System.out.print(cur.value + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void clear() {
        //粗暴实现：
        //head = null;
        ListNode cur = head;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur = null;
            cur = curNext;
        }
        head = null;
    }


    public ListNode partition(int x) {
        // write code here
        ListNode beforeList = null;
        ListNode afterList = null;
        ListNode beforeCur = null;
        ListNode afterCur = null;

        ListNode cur = head;
        while (cur != null) {
            if (cur.value < x) {
                if (beforeList == null) {
                    beforeList = cur;
                    beforeCur = cur;
                } else {
                    beforeCur.next = cur;
                    beforeCur = cur;
                }
            } else {
                if (afterList == null) {
                    afterList = cur;
                    afterCur = cur;
                } else {
                    afterCur.next = cur;
                    afterCur = cur;
                }
            }
            cur = cur.next;
        }
        if (beforeList == null) {
            return afterList;
        }
        beforeCur.next = afterList;
        if(afterList != null) {
            afterCur.next = null;
        }
        return beforeList;
    }

    public ListNode deleteDuplication() {
        if(head == null || head.next == null) {
            return head;
        }
        ListNode ret = head;
        ListNode pre = head;
        ListNode cur = head.next;

        boolean flag = false;
        if(pre.value == cur.value) {
            flag = true;
        }

        while(cur != null && cur.next != null) {
            if(cur.value != cur.next.value) {
                pre = pre.next;
                cur = cur.next;
            } else {
                int tmp = cur.value;
                while(cur != null && cur.value == tmp) {
                    cur = cur.next;
                }
                pre.next = cur;
            }
        }

        //处理开头有重复数字的情况
        if(flag == true) {
            while(ret != null && ret.value == head.value) {
                ret = ret.next;
            }
            return ret;
        }
        return ret;
    }
}
